package de.hauck.rtmidlet;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.io.Connection;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;

import de.hauck.rtmidlet.model.Configuration;
import de.hauck.rtmidlet.model.StockBean;
import de.hauck.rtmidlet.model.StockStore;
import de.hauck.rtmidlet.utils.Utils;
import de.hauck.rtmidlet.views.FormSettingsMenu;

public class FileSystemExportThread implements Runnable {
	static Configuration c;
	StockStore _myStore;
	static RTMidlet _main;
	FormSettingsMenu _fm;
	public FileSystemExportThread(RTMidlet rtmidlet, StockStore myStore, FormSettingsMenu fm) {
		_myStore = myStore;
		_main=rtmidlet;
		_fm = fm;
		new Thread(this).start();
	}

	public void run() {
		Enumeration drives = FileSystemRegistry.listRoots();
	      System.out.println("The valid roots found are: ");
	      while(drives.hasMoreElements()) {
	         String root = (String) drives.nextElement();
	         System.out.println("\t"+root);
	      }

		exportToCSV();	
	}
	
	protected void exportToCSV() {
		OutputStream os = null;
		FileConnection fileCon = null;
		//String url = c.exportURL;
		String url = "file:///"+c.exportPath+"RTMidlet.csv";
		try {
			Connection logCon = Connector.open(url, Connector.READ_WRITE);
			fileCon = (FileConnection) logCon;
			if (fileCon == null)
				throw new IOException("Couldn't open url " + url);
			if (!fileCon.exists()) {

				fileCon.create();
				System.out.println("writable: " + fileCon.canWrite());
			} else {
				fileCon.truncate(0);
				System.out.println("truncate: " + fileCon.getPath());
			}

			os = fileCon.openOutputStream();// openOutputStream();
			byte[] byte_array = "# data exported in format: ".getBytes();

			os.write(byte_array);
			os
					.write("Name, SymbolRT, SymbolTS, SymbolYahoo, SymbolFT, Amount, Invest, BuyDate, [Display in Realtime|Display in Depot]\n"
							.getBytes());
			Vector stockVec = _myStore.getStocksVector();
			StockBean stock = null;
			for (int i = 0; i < stockVec.size(); i++) {
				stock = (StockBean) stockVec.elementAt(i);
				os.write(stock.getName().getBytes());
				os.write(("," + stock.getSymbolRT()).getBytes());
				os.write(("," + stock.getSymbolTS()).getBytes());
				os.write(("," + stock.getSymbolYahoo()).getBytes());
				os.write(("," + stock.getSymbolFt()).getBytes());
				os.write(("," + stock.getAmount()).getBytes());
				os.write(("," + stock.getInvest()).getBytes());
				os.write(("," + Utils.convertDate2String(stock.getBuyDate()))
						.getBytes());
				os.write((","
						+ Utils.convertDisplayFlags2String(stock
								.getDisplayFlags()) + "\n").getBytes());
			}
			os.flush();
			_fm.showExportSuccess();
		} catch (IOException e) {
			_main.alert("Error", e.getMessage(), c.ALERT_WAIT_TIME);
			e.printStackTrace();
		} finally {
			try {
				if (os != null)
					os.close();
				if (fileCon != null)
					fileCon.close();

			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}

	}

}
